// scaup - Scala up!
// (c) 2010, Normen Müller, normen.mueller@gmail.com
// $Id: Logging.scala 125 2010-09-16 13:48:09Z normen.mueller $
package scaup.logging

import java.text.MessageFormat.{ format }
import org.slf4j.{ Logger, LoggerFactory }

/** @author <a href="mailto:normen.mueller@googlemail.com">Normen M\u00FCller</a>
 */
trait Loggable {
  val logger: Logger = Logging.getLogger(this)

  def checkFormat(msg: String, refs: Seq[Any]): String = if (refs.size > 0) format(msg, refs) else msg

  def trace(msg: String, refs: Any*) = logger trace checkFormat(msg, refs)

  def trace(t: Throwable, msg: String, refs: Any*) = logger trace (checkFormat(msg, refs), t)

  def info(msg: String, refs: Any*) = logger info checkFormat(msg, refs)

  def info(t: Throwable, msg: String, refs: Any*) = logger info (checkFormat(msg, refs), t)

  def warn(msg: String, refs: Any*) = logger warn checkFormat(msg, refs)

  def warn(t: Throwable, msg: String, refs: Any*) = logger warn (checkFormat(msg, refs), t)

  def critical(msg: String, refs: Any*) = logger error checkFormat(msg, refs)

  def critical(t: Throwable, msg: String, refs: Any*) = logger error (checkFormat(msg, refs), t)
}

/**
 *  <p><b>Note:</b> Initiated from <a href="http://scalax.scalaforge.org/">scalax</a></p>
 *
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M\u00FCller</a>
 */
object Logging {
  def loggerNameForClass(className: String) =
    if (className endsWith "$")
      className.substring(0, className.length - 1)
    else className

  def getLogger(logging: AnyRef) = LoggerFactory.getLogger(loggerNameForClass(logging.getClass.getName))

}
